var o = function () {
};
o.prototype = {
    _socket: null,
    _host: "",
    _conneted: !1,
    _heartBeatCount: 0,
    timeInterval: null,
    _isLogout: !1,
    ctor: function () {
        this._host = Global.config.host, this.initNet();
    },
    initNet: function () {
        console.log("initNet 初始化网络 host = " + this._host), this._socket = null, this._conneted = !1,
            this.stopSendHeartBeat(), cc.sys.platform == cc.sys.WECHAT_GAME ? (console.log("is cc.sys.WECHAT_GAME, use WX network!"),
            Global.TCP.connect(this._host)) : window.WebSocket ? (this._socket = new window.WebSocket(this._host),
            this._socket.onopen = this._onOpen.bind(this), this._socket.onclose = this._onClose.bind(this),
            this._socket.onmessage = this._onMessage.bind(this), this._socket.onerror = this._onError.bind(this)) : console.log("window.WebSocket == null!!");
    },
    _onOpen: function () {
        console.log("_onOpen", "网络连接成功!"), this._conneted = !0, Global.notificationCenter.emit(Global.eventName.GAME_NETWORK_CONNECT_SUCCESSED);
    },
    startSendHeartBeat: function () {
        this.timeInterval || (this.timeInterval = window.setInterval(this._sendHeartBeat.bind(this), 1e3));
    },
    stopSendHeartBeat: function () {
        console.warn(" ***************** stopSendHeartBeat ***************** "), this.timeInterval && (window.clearInterval(this.timeInterval),
            this.timeInterval = null);
    },
    _sendHeartBeat: function () {
        if (!Global._isInBackground) if (cc.sys.platform == cc.sys.WECHAT_GAME) Global.TCP.tickCount = (Global.TCP.tickCount + 1) % 10,
        9 == Global.TCP.tickCount && Global.TCP.connectStatus == Global.TCP.CONNECT_STATUS_OK && Global.notificationCenter.emit(Global.eventName.GAME_SEND_HERAT_BEAT); else {
            if (Global.notificationCenter.emit(Global.eventName.GAME_SEND_HERAT_BEAT), this._conneted) return;
            Global.notificationCenter.emit(Global.eventName.GAME_NETWORK_CONNECT_CLOSE);
        }
    },
    _onClose: function (e) {
        console.warn("_onClose", "网络连接关闭! err=", e), this._conneted = !1, Global.notificationCenter.emit(Global.eventName.GAME_NETWORK_CONNECT_CLOSE);
    },
    _onError: function (e) {
        console.warn("_onError", "网络连接关闭! err=", e), this._conneted = !1, Global.notificationCenter.emit(Global.eventName.GAME_NETWORK_CONNECT_FAILED);
    },
    _onMessage: function (e) {
        try {
            Global.notificationCenter.emit(Global.eventName.NET_RECIVE_MSG, e);
        } catch (e) {
        }
    },
    send: function (e) {
        console.info("network send = ", JSON.stringify(e)), cc.sys.platform == cc.sys.WECHAT_GAME ? Global.TCP.sendMsg(e) : this._socket && 1 == this._conneted ? this._socket.send(JSON.stringify(e)) : console.error("--------- 网络断开了 send 不了-------------");
    },
    close: function (e) {
        cc.sys.platform == cc.sys.WECHAT_GAME ? Global.TCP.close(e) : (this._conneted && this._socket.close(),
            this._socket = null, e && e());
    },
    closeAndReconnect: function (e) {
        var t;
        console.log("TCP closeAndReconnect:");
        var n = !1;
        cc.sys.platform == cc.sys.WECHAT_GAME ? (t = function () {
            Global.TCP.connect(Global.config.host);
        }.bind(this), n = Global.TCP.connectStatus == Global.TCP.CONNECT_STATUS_CLOSING || Global.TCP.connectStatus == Global.TCP.CONNECT_STATUS_FAIL) : (t = function () {
            Global.netWork.ctor();
        }, n = !this._conneted), n && (Global.persistNodeComps.setIsReconnecting(!0), this.close(t));
    },
    isConneted: function () {
        return cc.sys.platform == cc.sys.WECHAT_GAME && (this._conneted = Global.TCP.connectStatus == Global.TCP.CONNECT_STATUS_OK),
            this._conneted;
    },
    isLogout: function () {
        return this._isLogout;
    },
    setIsLogout: function (e) {
        this._isLogout = e;
    }
}, module.exports = o
